package com.hub.realtime.resource;


import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hub.realtime.resource.dto.input.ClusterInfoQueryParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hub.realtime.common.annotation.Log;
import com.hub.realtime.common.core.controller.BaseController;
import com.hub.realtime.common.core.domain.AjaxResult;
import com.hub.realtime.common.enums.BusinessType;
import com.hub.realtime.resource.domain.ResCluster;
import com.hub.realtime.resource.service.IResClusterService;
import com.hub.realtime.common.utils.poi.ExcelUtil;
import com.hub.realtime.common.core.page.TableDataInfo;

/**
 * 集群管理表Controller
 *
 * @author wupeng
 * @date 2021-12-30
 */
@RestController
@RequestMapping("/resource/cluster")
public class ResClusterController extends BaseController {
    @Autowired
    private IResClusterService resClusterService;

    /**
     * 查询集群管理表列表
     */
    @PreAuthorize("@ss.hasPermi('resource:cluster:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClusterInfoQueryParam param) {
        Page<ResCluster> page = resClusterService.list(param);
        return getDataTable(page.getRecords(),page.getTotal());
    }

    @GetMapping("/getCurrentClusterInfos")
    public TableDataInfo getCurrentClusterInfos() {
        List<ResCluster> list = resClusterService.getCurrentClusterInfos();
        return getDataTable(list,null);
    }

    /**
     * 导出集群管理表列表
     */
    @PreAuthorize("@ss.hasPermi('resource:cluster:export')")
    @Log(title = "集群管理表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClusterInfoQueryParam param) {
        List<ResCluster> list = resClusterService.list(param).getRecords();
        ExcelUtil<ResCluster> util = new ExcelUtil<ResCluster>(ResCluster.class);
        util.exportExcel(response, list, "集群管理表数据");
    }

    /**
     * 获取集群管理表详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:cluster:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(resClusterService.getBaseMapper().selectById(id));
    }

    /**
     * 新增集群管理表
     */
    @PreAuthorize("@ss.hasPermi('resource:cluster:add')")
    @Log(title = "集群管理表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResCluster resCluster) {
        return toAjax(resClusterService.saveInfo(resCluster));
    }

    /**
     * 测试集群连通性
     *
     * @param resCluster
     * @return
     */
    @PreAuthorize("@ss.hasPermi('resource:cluster:test')")
    @PostMapping("/test")
    public AjaxResult test(@RequestBody ResCluster resCluster) {
        return toAjax(resClusterService.clusterTest(resCluster));
    }

    /**
     * 修改集群管理表
     */
    @PreAuthorize("@ss.hasPermi('resource:cluster:edit')")
    @Log(title = "集群管理表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResCluster resCluster) {
        return toAjax(resClusterService.saveInfo(resCluster));
    }

    /**
     * 删除集群管理表
     */
    @PreAuthorize("@ss.hasPermi('resource:cluster:remove')")
    @Log(title = "集群管理表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(resClusterService.deleteInfos(ids));
    }
}

